package com.weidey.blog.controller;

import java.util.List;
import com.weidey.blog.domain.Article;
import com.weidey.blog.domain.Label;
import com.weidey.blog.service.IGroupService;
import com.weidey.blog.service.ILabelService;
import com.weidey.common.utils.SecurityUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.weidey.common.annotation.Log;
import com.weidey.common.core.controller.BaseController;
import com.weidey.common.core.domain.AjaxResult;
import com.weidey.common.enums.BusinessType;
import com.weidey.blog.service.IArticleService;
import com.weidey.common.utils.poi.ExcelUtil;
import com.weidey.common.core.page.TableDataInfo;

/**
 * 博客文章Controller
 * @author weidey
 */
@RestController
@RequestMapping("/blog/article")
public class ArticleController extends BaseController
{
    @Resource
    private IArticleService blogArticleService;
    @Resource
    private ILabelService blogLabelService;
    @Resource
    private IGroupService blogGroupService;

    /**
     * 查询博客文章列表
     */
    @PreAuthorize("@ss.hasPermi('blog:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(Article article)
    {
        startPage();
        List<Article> list = blogArticleService.selectBlogArticleList(article);
        return getDataTable(list);
    }

    /**
     * 导出博客文章列表
     */
    @PreAuthorize("@ss.hasPermi('blog:article:export')")
    @Log(title = "博客文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
        List<Article> list = blogArticleService.selectBlogArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<>(Article.class);
        util.exportExcel(response, list, "博客文章数据");
    }

    /**
     * 获取博客文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
         //查询博客文章详细信息
         Article article =  blogArticleService.selectBlogArticleById(id);
         //根据文章id查询博客文章标签列表
         List<Label> labels =  blogLabelService.selectBlogLabelListById(article.getId());
         article.setLabels(labels);
         //根据分类id查询博客文章分类
         article.setGroup(blogGroupService.selectBlogGroupById(article.getGroupId()));
        return success(article);
    }

    /**
     * 新增博客文章
     */
    @PreAuthorize("@ss.hasPermi('blog:article:add')")
    @Log(title = "博客文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Article article)
    {
        // 修复新增文章没有作者id
        Long userId = SecurityUtils.getUserId();
        article.setAuthorId(userId);
        int count = blogArticleService.insertBlogArticle(article);
        int [] tags  = article.getTags();
        if (tags.length>0){
            addTags(tags, article.getId());
        }
        return toAjax(count);
    }

    /**
     * 修改博客文章
     */
    @PreAuthorize("@ss.hasPermi('blog:article:edit')")
    @Log(title = "博客文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Article article)
    {
        int [] tags  = article.getTags();
        if (tags.length>0){
            addTags(tags, article.getId());
        }
        return toAjax(blogArticleService.updateBlogArticle(article));
    }

    /**
     * 删除博客文章
     */
    @PreAuthorize("@ss.hasPermi('blog:article:remove')")
    @Log(title = "博客文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids)
    {
        return toAjax(blogArticleService.deleteBlogArticleByIds(ids));
    }

    /**
     * 给文章添加标签
     * @param tags 标签id数组
     * @param id 文章id
     */
    private void addTags(int[] tags, Long id){
        blogArticleService.addTags(tags,id);
    }
}
